package com.prawn.ift.ctrlequipment.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentType;
import com.prawn.ift.ctrlequipment.service.CtrlEquipmentTypeService;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/23
 * @DESC： 控制设备类型管理
 */
@Controller
public class CtrlEquipmentTypeController {

    @Autowired
    private CtrlEquipmentTypeService ctrlEquipmentTypeService;

    /**
     * 添加控制设备类型信息
     * @Param ctrlEquipmentType
     * @return Result
     */
    @RequestMapping("/ctrlequipment/type/add")
    @ResponseBody
    public Result add(CtrlequipmentType ctrlEquipmentType) {
        ctrlEquipmentType.setId(UUID.randomUUID().toString().replaceAll("-",""));
        ctrlEquipmentType.setCreateDate(new Date());
        ctrlEquipmentType.setUpdateDate(new Date());
        return ctrlEquipmentTypeService.addCtrlequipmentType(ctrlEquipmentType);
    }


    /**
     * 删除控制设备类型信息
     * @Param id
     * @return Result
     */
    @RequestMapping("/ctrlequipment/type/delete")
    @ResponseBody
    public Result delete(String id) {
        return ctrlEquipmentTypeService.deleteCtrlequipmentType(id);
    }

    /**
     * 修改控制设备类型信息
     * @Param
     * @return Result
     */
    @RequestMapping("/ctrlequipment/type/edit")
    @ResponseBody
    public Result edit(CtrlequipmentType ctrlEquipmentType) {
        ctrlEquipmentType.setUpdateDate(new Date());
        ctrlEquipmentType.setCreateDate(null);
        return ctrlEquipmentTypeService.editCtrlequipmentType(ctrlEquipmentType);
    }

    /**
     * 查询控制设备类型信息
     * @return CtrlequipmentType
     */
    @RequestMapping("/ctrlequipment/type/{id}")
    @ResponseBody
    public CtrlequipmentType get(@PathVariable String id) {
        return ctrlEquipmentTypeService.getCtrlequipmentTypeById(id);
    }

    /**
     * 查询控制设备类型树信息
     * @return Result
     */
    @RequestMapping("/ctrlequipment/type/tree")
    @ResponseBody
    public Result getTree() {
        return ctrlEquipmentTypeService.getCtrlequipmentTypeTree();
    }

    /**
     * 查询控制设备类型树信息
     * @return Result
     */
    @RequestMapping("/ctrlequipment/type/tree/select")
    @ResponseBody
    public List<CtrlequipmentType> getTreeSelect() {
        return (List<CtrlequipmentType>) ctrlEquipmentTypeService.getCtrlequipmentTypeTree().getData();
    }

    /**
     * 查询控制设备类型信息列表
     * @return Result
     */
    @RequestMapping("/ctrlequipment/type/list")
    @ResponseBody
    public Result getList() {
        return ctrlEquipmentTypeService.getCtrlequipmentTypeList();
    }


    /**
     * 批量删除控制设备类型
     * @return Result
     */
    @RequestMapping("/ctrlequipment/type/batchDel")
    @ResponseBody
    public Result batchDel(@RequestBody String ids) {
        JSONObject jsonObject=(JSONObject) JSONObject.parse(ids);
        List<String> ids1 = jsonObject.getJSONArray("ids").toJavaList(String.class);

        return ctrlEquipmentTypeService.batchDelByIds(ids1);
    }


}
